package com.yuantu.judiciary.controller;

import com.yuantu.judiciary.annotation.Log;
import com.yuantu.judiciary.common.Response;
import com.yuantu.judiciary.common.ResponseFactory;
import com.yuantu.judiciary.model.enums.OrganizationType;
import com.yuantu.judiciary.model.form.*;
import com.yuantu.judiciary.model.vo.*;
import com.yuantu.judiciary.service.IVipInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * 套餐管理模块
 *
 * @author syw
 * @since 2023-08-11
 **/
@RestController
@RequestMapping(value = "/vip")
@Api(tags = "套餐管理模块")
@RequiredArgsConstructor
public class VipController extends BaseController {

    private final IVipInfoService vipInfoService;

    @Log(title = "添加套餐信息")
    @ApiOperation(value = "添加套餐信息")
    @PostMapping("/add")
    public Response<Boolean> add(@RequestBody @Validated VipInfoForm form) {
        return ResponseFactory.okResponse(vipInfoService.add(form));
    }

    @Log(title = "修改套餐信息")
    @ApiOperation(value = "修改套餐信息")
    @PostMapping("/modify")
    public Response<Boolean> modify(@RequestBody @Validated VipInfoForm form) {
        return ResponseFactory.okResponse(vipInfoService.modify(form));
    }

    @Log(title = "删除套餐信息")
    @ApiOperation(value = "删除套餐信息")
    @PostMapping("/delete")
    public Response<Boolean> delete(@RequestBody @NotEmpty List<Long> id) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(vipInfoService.delete(accountId, id));
    }

    @ApiOperation(value = "列表套餐信息")
    @GetMapping("/list")
    public Response<List<VipInfoForm>> list() {
        return ResponseFactory.okResponse(vipInfoService.list());
    }

    @ApiOperation(value = "获取支付宝小程序套餐信息")
    @GetMapping("/get/alipay-applet")
    public Response<VipInfoForm> getAlipayApplet() {
        return ResponseFactory.okResponse(vipInfoService.getAlipayApplet());
    }

    @ApiOperation(value = "app-获取套餐")
    @GetMapping("/get/by-type")
    public Response<List<VipInfoVO>> getByType(@RequestParam OrganizationType type) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(vipInfoService.getByType(type, accountId));
    }

    @ApiOperation(value = "app-是否购买套餐")
    @GetMapping("/verify/buy")
    public Response<Boolean> verifyBuy() {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(vipInfoService.verifyBuy(accountId));
    }

    @ApiOperation(value = "app-获取个人套餐")
    @GetMapping("/get/person")
    public Response<AccountVipInfoVO> getPerson() {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(vipInfoService.getPerson(accountId));
    }

    @ApiOperation(value = "管理端-获取企业套餐列表")
    @PostMapping("/get/org")
    public Response<PageVO<OrgVipVO>> getOrganization(@PageableDefault(sort = "createTime", direction = Sort.Direction.DESC) Pageable pageable,
                                                      @RequestBody @Validated SearchEnterVipFrom form) {
        return ResponseFactory.okResponse(vipInfoService.getOrganization(form, pageable));
    }

    @ApiOperation(value = "管理端-获取企业套餐使用情况")
    @PostMapping("/get/org/record")
    public Response<OrgRecordVO> getRecord(@RequestBody @Validated SearchEnterVipFrom form) {
        return ResponseFactory.okResponse(vipInfoService.getRecord(form));
    }


    @ApiOperation(value = "管理端-获取企业套餐使用列表")
    @PostMapping("/get/org/record/list")
    public Response<PageVO<OrgRecordListVO>> getRecordList(@PageableDefault(sort = "createTime", direction = Sort.Direction.DESC) Pageable pageable,
                                                           @RequestBody @Validated SearchEnterVipRecordFrom form) {
        return ResponseFactory.okResponse(vipInfoService.getRecordList(form, pageable));
    }

    @Log(title = "添加套餐使用信息")
    @ApiOperation(value = "管理端-添加套餐使用信息")
    @PostMapping("/add/record")
    public Response<Boolean> addRecord(@RequestBody @Validated AddVipRecordForm form) {
        return ResponseFactory.okResponse(vipInfoService.addRecord(form));
    }

    @Log(title = "修改套餐使用信息")
    @ApiOperation(value = "管理端-修改套餐使用信息")
    @PostMapping("/modify/record")
    public Response<Boolean> modifyRecord(@RequestBody @Validated ModifyVipRecordForm form) {
        return ResponseFactory.okResponse(vipInfoService.modifyRecord(form));
    }

    @Log(title = "删除套餐使用信息")
    @ApiOperation(value = "管理端-删除套餐使用信息")
    @PostMapping("/delete/record")
    public Response<Boolean> deleteRecord(@RequestBody @NotEmpty List<Long> id) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(vipInfoService.deleteRecord(accountId, id));
    }

    @ApiOperation(value = "管理端-获取套餐使用信息列表")
    @PostMapping("/list/record")
    public Response<PageVO<VipRecordVO>> listRecord(@PageableDefault(sort = "createTime", direction = Sort.Direction.DESC) Pageable pageable,
                                                    @RequestBody @Validated SearchAccountForm form) {
        return ResponseFactory.okResponse(vipInfoService.listRecord(form, pageable));
    }

}
